package com.jetpack.mvvm.lib.binding.ext

import android.app.Activity
import android.app.Dialog
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.Lifecycle
import androidx.recyclerview.widget.RecyclerView
import androidx.viewbinding.ViewBinding
import com.jetpack.mvvm.lib.binding.viewbind.*

inline fun <reified T : ViewBinding> Activity.viewBind() = ActivityViewBinding(T::class.java, this)

inline fun <reified T : ViewBinding> Fragment.viewBind() = FragmentViewBinding(T::class.java, this)

inline fun <reified T : ViewBinding> Dialog.viewBind() = DialogViewBinding(T::class.java)
inline fun <reified T : ViewBinding> Dialog.viewBind(lifecycle: Lifecycle) = DialogViewBinding(T::class.java, lifecycle)

inline fun <reified T : ViewBinding> RecyclerView.ViewHolder.viewBind() = ViewHolderViewBinding(T::class.java)

inline fun <reified T : ViewBinding> ViewGroup.viewBind(merge: Boolean = true) = ViewGroupViewBinding(
    clazz = T::class.java,
    inflater = LayoutInflater.from(this.context),
    viewGroup = this,
    isMerge = merge  // layout xml 的最外层的 tag 是否为 <merge />
)

inline fun <reified T : ViewBinding> ViewGroup.viewBind(viewGroup: ViewGroup) =
    ViewGroupViewBinding(
        clazz = T::class.java,
        inflater = LayoutInflater.from(context),
        viewGroup = viewGroup,
    )